package com.war.helpers;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Map.Entry;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

/**
 * Adding methods for help the developer.
 *
 * @author Ambroise
 */
@ManagedBean(name="helperDebug")
@RequestScoped
public class HelperDebug {

	/**
	 * Display a HashMap in HTML content.
	 * 
	 * @param H - Hasm HhMap - The HashMap to display.
	 * @param nameHashMap - String - The HashMap's label.
	 */
	public static void displayHashMapHtml(HashMap H, String nameHashMap) {
		// Set est l'interface d'une liste sans doublon
		// Cette liste contient l'ensemble des pairs clef-valeur
		Set<Entry<Object, Object>> entrySet = H.entrySet();
		
		// On crée un itérateur sur la "Set"
		Iterator<Entry<Object, Object>> it = entrySet.iterator();

		String content = "<u>Affichage de la HashMap "+nameHashMap+"</u><br />";
		while (it.hasNext()) {
			Entry<Object, Object> entry = it.next();
			
			content += entry.getKey() + " ("+entry.getKey().getClass().getSimpleName()+")" + " = ";
			if(entry.getValue().getClass().isPrimitive() || entry.getValue().getClass().equals(String.class)){
				content += entry.getValue()+ " ("+entry.getValue().getClass().getSimpleName()+")<br />";
			}else if(entry.getValue().getClass().isArray()){
				content += "Array"+Arrays.deepToString((Object[]) entry.getValue())+"<br />";
			}else{
				content += entry.getValue().toString()+" ( "+entry.getValue().getClass()+")<br />";
			}
			
		}
		
		// Add the content to display.
		HelperMessageHtml helper = new HelperMessageHtml();
		helper.addMessage(content, "info", true); 
		
	}
    
    public static void addMessage(String message, String type){
        HelperMessageHtml helper = new HelperMessageHtml();
		helper.addMessage(message, type, false); 
    }
	
	public void test(){
		HashMap<String, Object> hhh = new HashMap<String, Object>();
		ArrayList<Object> test = new ArrayList<Object>();
		test.add("Coucou");
		hhh.put("type", "block");
		hhh.put("message", "Test !");
		hhh.put("Test", test);
		displayHashMapHtml(hhh, "test");
	}
}
